package interfaceExercises.no2_;


import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
	static Scanner scanner=new Scanner(System.in);
	static double a,b;
	public static void main(String[] args) {
		ShapeCalculator shapeCalculator;
		int choice;
		while (true){
			try {
				System.out.println("请选择形状");
				System.out.println("1.圆形");
				System.out.println("2.矩形");
				System.out.println("0.退出");
				choice=scanner.nextInt();

				switch (choice){
					case 1:
						System.out.println("请输入圆的半径");
						a=scanner.nextDouble();
						shapeCalculator=new ShapeCalculator() {
							@Override
							public double area() {
								double radius=Main.a;
								return Math.PI*radius*radius;
							}
							@Override
							public double circumference() {
								double radius=Main.a;
								return Math.PI*radius*radius;
							}
						};
						break;
					case 2:
						System.out.println("请输入矩形的长和宽");
						a=scanner.nextDouble();
						b=scanner.nextDouble();
						shapeCalculator=new ShapeCalculator() {
							@Override
							public double area() {
								return Main.a*Main.b;
							}

							@Override
							public double circumference() {
								return 2*(Main.a+ Main.b);
							}
						};
						break;
					case 0:
						return;
					default:
						System.out.println("输入数字无效");
						continue;
				}
				System.out.println("面积为"+shapeCalculator.area());
				System.out.println("周长为"+shapeCalculator.circumference());
			}
			catch (InputMismatchException inputMismatchException){
				System.out.println("输入数字无效");
			}
		}
	}
}
interface ShapeCalculator{
	double area();
	double circumference();
}
